package service

import (
	"singo/model"
	"singo/serializer"
)

// ListOrganizationService 项目列表服务
type ListOrganizationService struct {
	Limit int `form:"pageSize"`
	Start int `form:"pageNo"`
}

// OrganizationList 项目列表
func (service *ListOrganizationService) OrganizationList() serializer.Response {
	organizations := []model.Organization{}
	total := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if err := model.DB.Model(&organizations).Count(&total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Find(&organizations).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	return serializer.BuildListResponse(serializer.BuildOrganizations(organizations), uint(total))
}
